local audio = require("framework.audio")
local audioPath = require("app.res.audio.config")
local recordManger = require("app.manger.recordManger")
local ImgNumber = require("app.views.imgNumber")
local M = class("resultDialog",function ( ... )
	return display.newLayer()
end)

local TextTab = {
    ["0"] = "#text_0.png",
    ["1"] = "#text_1.png",
    ["2"] = "#text_2.png",
    ["3"] = "#text_3.png",
    ["4"] = "#text_4.png",
    ["5"] = "#text_5.png",
    ["6"] = "#text_6.png",
    ["7"] = "#text_7.png",
    ["8"] = "#text_8.png",
    ["9"] = "#text_9.png",
    [":"] = "#text_mao.png",
}


local posY = display.cy+40
function M:ctor(data)
	self.m_share = data.share
	self.restart = data.restart
	self.maskBg = display.newColorLayer(cc.c4b(0,0,0,180))
		:addTo(self)
	self.maskBg:setCascadeOpacityEnabled(true)
	self.bg = display.newSprite("#game_09.png")
				:addTo(self)
				:align(display.CENTER, display.cx+display.width, posY)
	local size = self.bg:getContentSize()
	local btn_share = cc.ui.UIPushButton.new{normal = "#game_07.png",pressed = "#game_08.png"}
				:addTo(self.bg)
				:align(display.CENTER_BOTTOM, size.width*0.45, -20)
				:onButtonPressed(function (  )
			        audio.playSound(audioPath.button)
			    end)
				:onButtonClicked(function ( ... )
					self:runOut()
				end)
	local btn_restart = cc.ui.UIPushButton.new{normal = "#game_15.png",pressed = "#game_17.png"}
				:addTo(self.bg)
				:align(display.CENTER_BOTTOM, size.width*0.76, -40)
				:onButtonPressed(function (  )
			        audio.playSound(audioPath.button)
			    end)
				:onButtonClicked(function ( ... )
					self:runOut()
				end)

	self.curScoreLabel = ImgNumber:new(TextTab)
        :color(cc.c3b(221,43,14))
        :addTo(self.bg)
        :pos(400,190)
        :scale(0.3)
        :setNumber(data.curScore)

    self.heightScoreLabel = ImgNumber:new(TextTab)
        :color(cc.c3b(14,93,149))
        :addTo(self.bg)
        :pos(285,152)
        :scale(0.22)
        :setNumber(recordManger:getHeightScore())

    self.percentLabel = ImgNumber:new(TextTab)
        :color(cc.c3b(255,215,0))
        :addTo(self.bg)
        :pos(290,114)
        :scale(0.2)
        :setNumber(00)

	self:runIn()
end

function M:runIn()

	local ani = transition.sequence{cc.MoveTo:create(0.5, cc.p(display.cx-60, posY)),cc.CallFunc:create(function ( ... )
		audio.stopMusic(false)
        audio.playMusic(audioPath.beginBg)
	end),cc.MoveTo:create(0.1, cc.p(display.cx, posY))}
	self.bg:runAction(ani)
	self.maskBg:runAction(transition.sequence({cc.FadeOut:create(0),cc.FadeIn:create(0.4)}))
end

function M:runOut()
	local seq = transition.sequence({cc.MoveTo:create(0.1, cc.p(display.cx-100, posY)),
							cc.CallFunc:create(function ( ... )
                                              	if self.restart then
	                                              	if self.restart and type(self.restart)=="function" then
														self.restart()
													end
                                              	end
                                          end),cc.MoveTo:create(0.5, cc.p(display.cx+display.width, posY)),
											cc.CallFunc:create(function ( ... )
												self:removeSelf()
                                          end)})
	self.bg:runAction(seq)
	self.maskBg:runAction(cc.FadeOut:create(0.6))
end

return M